package com.dzt.androidnewui.fragment;

import android.app.Activity;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.dzt.androidnewui.R;
import com.dzt.androidnewui.utils.JLogKit;
import com.dzt.androidnewui.utils.PerfectClickListener;

/**
 * Created by dzt on 2017/9/15.
 * Fragment基类，无标题，标题都在Activity中显示
 */

public abstract class BaseFragment<SV extends ViewDataBinding> extends Fragment {
	protected Activity context;
	// 布局view
	protected SV bindingView;
	// fragment是否显示了
	protected boolean mIsVisible = false;
	// 加载中
	private LinearLayout mLlProgressBar;
	// 加载失败
	private LinearLayout mRefresh;
	// 内容布局
	protected RelativeLayout mContainer;
	// 动画
	private AnimationDrawable mAnimationDrawable;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View ll = inflater.inflate(R.layout.fragment_base, null);
		bindingView = DataBindingUtil.inflate(getActivity().getLayoutInflater(), setContent(), null, false);
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
		bindingView.getRoot().setLayoutParams(params);
		mContainer = ll.findViewById(R.id.container);
		mContainer.addView(bindingView.getRoot());
		JLogKit.getInstance().e("fragment create view");
		return ll;
	}

	/**
	 * 在这里实现Fragment数据的缓加载.
	 */
	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		super.setUserVisibleHint(isVisibleToUser);
		if (getUserVisibleHint()) {
			mIsVisible = true;
			onVisible();
		} else {
			mIsVisible = false;
			onInvisible();
		}
	}

	protected void onInvisible() {
		JLogKit.getInstance().e("onInvisible");
	}

	/**
	 * 显示时加载数据,需要这样的使用
	 * 注意声明 isPrepared，先初始化
	 * 生命周期会先执行 setUserVisibleHint 再执行onActivityCreated
	 * 在 onActivityCreated 之后第一次显示加载数据，只加载一次
	 */
	protected void loadData() {
		JLogKit.getInstance().e("loadData");
	}

	protected void onVisible() {
		JLogKit.getInstance().e("onVisible");
		loadData();
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		JLogKit.getInstance().e("onActivityCreated");
		context = getActivity();
		mLlProgressBar = getView(R.id.ll_progress_bar);
		ImageView img = getView(R.id.img_progress);

		// 加载动画
		mAnimationDrawable = (AnimationDrawable) img.getDrawable();
		// 默认进入页面就开启动画
		if (!mAnimationDrawable.isRunning()) {
			mAnimationDrawable.start();
		}
		mRefresh = getView(R.id.ll_error_refresh);
		// 点击加载失败布局
		mRefresh.setOnClickListener(new PerfectClickListener() {
			@Override
			protected void onNoDoubleClick(View v) {
				showLoading();
				onRefresh();
			}
		});
		bindingView.getRoot().setVisibility(View.GONE);
	}

	protected <T extends View> T getView(int id) {
		return (T) getView().findViewById(id);
	}

	/**
	 * 布局
	 */
	public abstract int setContent();

	/**
	 * 加载失败后点击后的操作
	 */
	protected void onRefresh() {
		JLogKit.getInstance().e("onRefresh");
	}

	/**
	 * 显示加载中状态
	 */
	protected void showLoading() {
		if (mLlProgressBar.getVisibility() != View.VISIBLE) {
			mLlProgressBar.setVisibility(View.VISIBLE);
		}
		// 开始动画
		if (!mAnimationDrawable.isRunning()) {
			mAnimationDrawable.start();
		}
		if (bindingView.getRoot().getVisibility() != View.GONE) {
			bindingView.getRoot().setVisibility(View.GONE);
		}
		if (mRefresh.getVisibility() != View.GONE) {
			mRefresh.setVisibility(View.GONE);
		}
	}

	/**
	 * 加载完成的状态
	 */
	protected void showContentView() {
		if (mLlProgressBar.getVisibility() != View.GONE) {
			mLlProgressBar.setVisibility(View.GONE);
		}
		// 停止动画
		if (mAnimationDrawable.isRunning()) {
			mAnimationDrawable.stop();
		}
		if (mRefresh.getVisibility() != View.GONE) {
			mRefresh.setVisibility(View.GONE);
		}
		if (bindingView.getRoot().getVisibility() != View.VISIBLE) {
			bindingView.getRoot().setVisibility(View.VISIBLE);
		}
	}

	/**
	 * 加载失败点击重新加载的状态
	 */
	protected void showError() {
		if (mLlProgressBar.getVisibility() != View.GONE) {
			mLlProgressBar.setVisibility(View.GONE);
		}
		// 停止动画
		if (mAnimationDrawable.isRunning()) {
			mAnimationDrawable.stop();
		}
		if (mRefresh.getVisibility() != View.VISIBLE) {
			mRefresh.setVisibility(View.VISIBLE);
		}
		if (bindingView.getRoot().getVisibility() != View.GONE) {
			bindingView.getRoot().setVisibility(View.GONE);
		}
	}
}
